package Logica;
import grafico.Ventana;

import java.util.Hashtable;
import jpl.*;
import jpl.Integer;


public class Interfaz {
	
	protected static Interfaz instance;
	String t1= "consult('juegoGo.pl')";
	Query q1;
	int Jb,Jn;
	String T="";
	
	 public static Interfaz getInstance() {

         if (instance == null)
                 instance = new Interfaz();

         return instance;
 }
	 private Interfaz() {
		 q1 = new Query(t1);
		 q1.hasSolution();
 }
	 
	 
	 
	 public boolean hayFicha(int x,int y,char c)
	 {
		 	return T.contains("pos("+x+","+y+","+c+")");
	 }
	 
	public boolean insertar(int x, int y, char c) {
					
		String XenSn,YenSn;
		XenSn="pasarASN("+x+",PX)";
		YenSn="pasarASN("+y+",PY)";
		String X,Y;
		q1=new Query(t1);
		q1=new Query(XenSn);
		X= q1.oneSolution().get("PX").toString();
		q1=new Query(YenSn);
		Y= (String)q1.oneSolution().get("PY").toString();
		q1=new Query("agregar("+X+" , "+Y+" , "+c+" ,P,T)");
		String j;
	
		Hashtable x4=q1.oneSolution();
		if(x4!=null)
			{int I=((Integer)x4.get("P")).intValue();
				if(c=='b')
					Jb+=I;
				else Jn+=I;
			Ventana.getInstance().actualizar(Jb, Jn);
			T= x4.get("T").toString().replaceAll(" ","");
			}
		
		return x4!=null;
		
	}
	
	public void resetear()
	{
		q1=new Query("resetear");
		q1.hasSolution();
		T="";
		Jb=0;
		Jn=0;
		Ventana.getInstance().actualizar(Jb, Jn);
		
	}

}